//tool.ts
import type { ComponentInternalInstance } from "vue"

const rootComponentName = "KxTable" //根节点Name
/**
 * @description 获取根节点
 * @param {ComponentInternalInstance} component 当前节点
 * @returns 根节点
 */
export const getRootComponent = (component: ComponentInternalInstance | null): ComponentInternalInstance | null => {
  //若当前节点已是根节点 则直接返回当前节点
  if (component && component.type.name === rootComponentName) {
    return component
  }
  //当前节点有节点 递归查询 找到根节点
  if (component && component.parent) {
    const parent = component.parent
    return getRootComponent(parent)
  }
  return null
}
//获取唯一标识
export const generateUUID = () => {
  let d = new Date().getTime()
  return "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(/[xy]/g, function (c) {
    const r = (d + Math.random() * 16) % 16 | 0
    d = Math.floor(d / 16)
    return (c === "x" ? r : (r & 0x3) | 0x8).toString(16)
  })
}
